import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { GitConfig, SubModule ,DockerfileCreate} from '@api/git';
import { GitManagerService } from '@common/services/git/gitmanager';

@Component({
  selector: 'kd-docker-config',
  templateUrl: './docker-config.component.html',
  styleUrls: ['./docker-config.component.scss']
})
export class DockerConfigComponent implements OnInit {

  constructor( private readonly git_: GitManagerService,
    private readonly _ref :ChangeDetectorRef) { }
  options = {
    theme: 'vs-dark',
    readOnly : false ,
    language: 'dockerfile'
  };
  code = "hello world"
  select_i:number = 0;
  _gitid :number = -1;
  displayedColumns: string[] = ['id', 'name', 'url', 'path', 'authid','status'];
  dataSource: GitConfig[]=[];
  _modules :SubModule[]=[]
  ngOnInit(): void {
    this.git_.getGitConfig().subscribe(
      data => {
        this.dataSource = data
      }
    )
  }

  GetSubModule(gitid:number){
    this._gitid = gitid
    this.git_.SubModuleList(gitid).subscribe(
      data => this._modules = data
    )
  }
  ChangeModule(index : number){
    this.select_i = index
    this._ref.markForCheck()
    this.git_.SubModuleDockerfile(this._gitid,this._modules[index].name).subscribe(
      data => {
        if(!data.exist){
          this.code ="this is sub module doesn't have a  Dockerfile"
        }else{
          this.code = data.dockerfile
        }
      }
    )
  }

  CreateOrUpdateDockerfile(){
    var data :DockerfileCreate= {
      resp: {
        exist: true,
        dockerfile: this.code
      },
      req: {
        gitid: this._gitid,
        submodule: this._modules[this.select_i].name
      }
    } 
    this.git_.SubModuleDockerfileCreate(data).subscribe(
      _data => console.log("update finish!",_data)
    )
      return 
  }
  AutoGenerationDockerfile(){
    this.git_.SubModuleAutoGenDockerfile(this._gitid,this._modules[this.select_i].name).subscribe(
      data => {
        if(!data.exist){
          this.code ="this is sub module doesn't have a  Dockerfile"
        }else{
          this.code = data.dockerfile
        }
      }
    )
  }
}
